package faceb.server.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import faceb.server.dao.ResourceDao;
import faceb.server.model.Resource;
import faceb.server.util.MyBatisUtil;

@Repository("resourceDao")
public class ResourceDaoImpl implements ResourceDao {
	private SqlSessionFactory factory = MyBatisUtil.getsqlSessionFactory();

	public int insertResource(Resource resource) {
		SqlSession session = factory.openSession();
		int result = 0;
		try {
			result = session.insert("ResourceMapper.insertResource", resource);
			session.commit();
		} catch (Exception e) {
			result = 0;
		}
		session.close();
		return result;
	}

	public Resource getResourceById(String resourceid) {
		SqlSession session = factory.openSession();
		Resource resource = session.selectOne("ResourceMapper.getResourceById",
				resourceid);
		session.close();
		return resource;
	}

	public List<Resource> getResourcesByUserid(String userid) {
		SqlSession session = factory.openSession();
		List<Resource> resources = session.selectList(
				"ResourceMapper.getResourcesByUserid", userid);
		session.close();
		return resources;
	}

	public static void main(String[] args) {
		List<Resource> resources = new ResourceDaoImpl()
				.getResourcesByUserid("1");

		for (Resource r :resources) {
			System.out.println(r.getR_name());
		}
	}
}
